package com.example.smartcity.fragment;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.smartcity.R;
import com.example.smartcity.activity.AdviceContentActivity;
import com.example.smartcity.activity.NewsWebViewActivity;
import com.example.smartcity.bean.AdviceBean;
import com.example.smartcity.bean.DongtaiBean;
import com.example.smartcity.utils.APIConfig;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class JDongtaiFragment extends BaseFragment {
    private List<String> list;
    private ListView jd_lv;
    private static final String TAG = JDongtaiFragment.class.getSimpleName();
    String result;
    private static DongtaiBean dongtaiBean;
    @Override
    public View initView() {
        Log.i(TAG,"建党动态");
        View view = View.inflate(getContext(), R.layout.jd_dongtai, null);
        jd_lv = view.findViewById(R.id.jd_lv);
        jd_lv.setAdapter(new JDongtaiAdapter(getContext()));
        jd_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getContext(),"你点击了"+(position+1),Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }

    @Override
    public void initData() {
        super.initData();

    }
    private void getData(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(APIConfig.BASE_URL + "/dongtai/getall")
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.i("onFailure",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    result = response.body().string();
                    Log.i("dongtai_Request_Success:",result);
                    Gson gson = new Gson();
                    dongtaiBean =  gson.fromJson(result, DongtaiBean.class);

                    Log.i("JDDT", dongtaiBean.getDTList().toString());//todo
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            JDongtaiAdapter dtAdapter = new JDongtaiAdapter(getActivity());
                            jd_lv.setAdapter(dtAdapter);
                            jd_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                    Intent intent = new Intent(getActivity(), NewsWebViewActivity.class);
                                    ImageView jd_img = view.findViewById(R.id.jd_img);
                                    jd_img.setDrawingCacheEnabled(Boolean.TRUE);
                                    intent.putExtra("bitmap",jd_img.getDrawingCache());
                                    intent.putExtra("title",dongtaiBean.getDTList().get(i).getTitle());
                                    intent.putExtra("time",dongtaiBean.getDTList().get(i).getTime());
                                    intent.putExtra("content",dongtaiBean.getDTList().get(i).getContent());
                                    getActivity().startActivity(intent);
                                }
                            });

                        }
                    });
                }
            }
        });
    }

    public static class JDongtaiAdapter extends BaseAdapter {
        private Context context;
        private LayoutInflater layoutInflater;

        public JDongtaiAdapter(Context context) {
            this.context = context;
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return dongtaiBean !=null ? dongtaiBean.getDTList().size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return dongtaiBean.getDTList().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 实例化ViewHolder
            ViewHolder viewHolder = new ViewHolder();
            if (convertView==null){
                convertView = layoutInflater.inflate(R.layout.jd_display_item,null);
                // 生成一个ViewHolder对象
                viewHolder = new ViewHolder();
                // 绑定控件一3以便赋值
                viewHolder.jd_content = convertView.findViewById(R.id.jd_context);
                viewHolder.jd_date = convertView.findViewById(R.id.jd_date);
                viewHolder.jd_img = convertView.findViewById(R.id.jd_img);
                viewHolder.jd_title = convertView.findViewById(R.id.jd_title);
                // 把viewHolder传递进去
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.jd_title.setText(dongtaiBean.getDTList().get(position).getTitle());
            viewHolder.jd_content.setText(dongtaiBean.getDTList().get(position).getContent());
            viewHolder.jd_date.setText(dongtaiBean.getDTList().get(position).getTime());
            String url = dongtaiBean.getDTList().get(position).getUrl();
            Glide.with(context).load(url).into(viewHolder.jd_img);
            return convertView;
        }


        //写一个静态的class, 把layout_list_item的控件转移过来使用
        static class ViewHolder{
            public TextView jd_title;
            public TextView jd_content;
            public TextView jd_date;
            public ImageView jd_img;
        }
    }

}
